import utils from '@/utils/util'

// 合并歌曲到播放列表查重
const concatPlayList = (list, playList = []) => {
  // filter过滤无版权及vip歌曲
  return utils.concatPlayList(list.filter(item => { return !item.license && !item.vip }), playList)
}

// 当前歌曲在播放列表的索引
const findIndex = (list, playList) => {
  return playList.findIndex(d => { return d.id === list.id })
}

export default {
  loginSuc({ commit }, val) {
    commit('setLoginDialog', val)
  },
  // 播放歌曲列表里全部歌曲（清空当前播放列表）
  playAll({ commit }, { list }) {
    commit('SET_PLAYLIST', concatPlayList(list))
    commit('SET_PLAYSTATUS', true)
    commit('SET_PLAYINDEX', 0)
  },
  // 播放当前选中的歌曲
  selectPlay({ commit, state }, { list }) {
    const playList = concatPlayList(list, state.playList)

    commit('SET_PLAYLIST', playList)
    commit('SET_PLAYSTATUS', true)
    commit('SET_PLAYINDEX', findIndex(list[0], playList))
  },
  // 添加歌曲到当前播放列表
  addList({ commit, state }, { list }) {
    const playList = concatPlayList(list, state.playList)
    commit('SET_PLAYLIST', playList)
    commit('SET_PLAYLISTTIPS', true);
  }
}
